package com.peaker.advertisingpro.activity

import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.SeekBar
import com.google.gson.Gson
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.base.BaseActivity
import com.peaker.advertisingpro.utils.Common
import com.peaker.advertisingpro.utils.SPUtil
import com.peaker.advertisingpro.utils.SmdtManagerUtils
import com.peaker.advertisingpro.utils.ToastUtils
import kotlinx.android.synthetic.main.dialog_boot_layout.*
import kotlinx.android.synthetic.main.dialog_plan_layout_new.*
import kotlinx.android.synthetic.main.dialog_screen_rotation_layout.*
import kotlinx.android.synthetic.main.dialog_system_layout.*
import kotlinx.android.synthetic.main.dialog_volume_layout.*
import kotlinx.android.synthetic.main.layout_setname.*
import kotlinx.android.synthetic.main.layout_setpwd.*

class ActionActivity : BaseActivity(), View.OnClickListener {
    var volume: Int = 0
    var b_angle: Int = 0
    var model: Boolean = false
    var b_reboot: Boolean = false
    var b_protect: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var layout = intent.getIntExtra("layout", R.layout.activity_action)
        setContentView(layout)

        when (layout) {
            R.layout.layout_setname -> {
                btn_back.setOnClickListener(this)
                btn_save.setOnClickListener(this)
                btn_cancel.setOnClickListener(this)
                var name =
                    if (TextUtils.isEmpty(SPUtil.getSP(Common.MACNAME))) SmdtManagerUtils.smdtGetEthMacAddress() else SPUtil.getSP(
                        Common.MACNAME
                    )
                et_name_set.setText(name)

            }
            R.layout.dialog_volume_layout -> {
                seekV.progress = SmdtManagerUtils.getCurrentSysVolume()
                btn_back_volum.setOnClickListener(this)
                btn_save_volume.setOnClickListener(this)
                btn_cancel_volume.setOnClickListener(this)
                currentV.text = "当前音量: ${SmdtManagerUtils.getCurrentSysVolume()}"
                seekV.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                       var progress1=  SmdtManagerUtils.smdtgetAndroidVolume(progress)
                        volume = progress1
                        currentV.text = "当前音量: $progress1"
                    }

                    override fun onStartTrackingTouch(seekBar: SeekBar?) {}
                    override fun onStopTrackingTouch(seekBar: SeekBar?) {}
                })
            }
            //开关机时间
            R.layout.dialog_plan_layout_new -> {
                btn_cancel_plan.setOnClickListener(this)
                btn_back_plan.setOnClickListener(this)
                btn_set_cls.setOnClickListener(this)
                btn_save_plan.setOnClickListener(this)
                var on = if (TextUtils.isEmpty(SPUtil.getSP(Common.BOOTTIME))) "" else SPUtil.getSP(Common.BOOTTIME)
                var off = if (TextUtils.isEmpty(SPUtil.getSP(Common.SHUTDOWN))) "" else SPUtil.getSP(Common.SHUTDOWN)
                if(on.isNullOrEmpty() or off.isNullOrEmpty())return
                var split = on?.split(":")
                var splitOff = off?.split(":")
                on_h.setText(split?.get(0))
                on_m.setText(split?.get(1))
                off_h.setText(splitOff?.get(0))
                off_m.setText(splitOff?.get(1))
            }
            R.layout.dialog_system_layout -> {//联机单机模式设置
                btn_save_model.setOnClickListener(this)
                btn_cancel_model.setOnClickListener(this)
                btn_back_model.setOnClickListener(this)
                var moudle = if (TextUtils.isEmpty(SPUtil.getSP(Common.MOUDLE))) "联机模式" else SPUtil.getSP(Common.MOUDLE)
                if (moudle == "联机模式") {
                    rb_online.isChecked = true
                    model = false
                } else {
                    rb_offline.isChecked = true
                    model = true
                }
                rg.setOnCheckedChangeListener { group, checkedId ->
                    when (checkedId) {
                        R.id.rb_offline -> { //单机
                            model = true
                        }
                        R.id.rb_online -> {//联机
                            model = false
                        }
                    }
                }
            }

            R.layout.dialog_boot_layout -> {//启动模式设置
                btn_save_reboot.setOnClickListener(this)
                btn_cancel_reboot.setOnClickListener(this)
                btn_back_reboot.setOnClickListener(this)
                var reboot = if (TextUtils.isEmpty(SPUtil.getSP(Common.REBOOT))) "关" else SPUtil.getSP(Common.REBOOT)
                var protect = if (TextUtils.isEmpty(SPUtil.getSP(Common.PROTECT))) "关" else SPUtil.getSP(Common.PROTECT)

                if (protect == "关") {
                    rb_protect_off.isChecked = true
                    b_protect =false
                } else {
                    rb_protect_on.isChecked = true
                    b_protect =true
                }
                if (reboot == "关") {
                    rb_boot_off.isChecked = true
                    b_reboot =false
                } else {
                    rb_boot_on.isChecked = true
                    b_reboot=true
                }
                rg_protect.setOnCheckedChangeListener { group, checkedId ->
                    when (checkedId) {
                        R.id.rb_protect_on -> {
                            b_protect = true
                        }
                        R.id.rb_protect_off -> {
                            b_protect = false
                        }
                    }

                }
                rg_boot.setOnCheckedChangeListener { group, checkedId ->
                    //rb_boot_on
                    when (checkedId) {
                        R.id.rb_boot_on -> {
                            b_reboot = true
                        }
                        R.id.rb_boot_off -> {
                            b_reboot = false
                        }
                    }
                }

            }
            //设置密码
            R.layout.layout_setpwd -> {
                btn_save_pwd.setOnClickListener(this)
                btn_cancel_pwd.setOnClickListener(this)
                btn_back_pwd.setOnClickListener(this)
                var pwd = SPUtil.getSP(Common.PWD)
                pwd_et.setText(pwd)
            }
            //屏幕旋转角度
            R.layout.dialog_screen_rotation_layout -> {
                btn_save_screen.setOnClickListener(this)
                btn_cancel_screen.setOnClickListener(this)
                btn_back_screen.setOnClickListener(this)
                var angle = if (TextUtils.isEmpty(SPUtil.getSP(Common.ANGLE))) "0" else SPUtil.getSP(Common.ANGLE)
                when (angle) {
                    "0" -> rb1_rotation_0.isChecked = true
                    "90" -> rb1_rotation_90.isChecked = true
                    "180" -> rb1_rotation_180.isChecked = true
                    "270" -> rb1_rotation_270.isChecked = true
                }
                rg_screen1.setOnCheckedChangeListener { group, checkedId ->
                    when (checkedId) {
                        R.id.rb1_rotation_0 -> b_angle = 0
                        R.id.rb1_rotation_90 -> b_angle = 90
                        R.id.rb1_rotation_180 -> b_angle = 180
                        R.id.rb1_rotation_270 -> b_angle = 270
                    }
                }

            }
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_back, R.id.btn_back_volum, R.id.btn_cancel, R.id.btn_cancel_volume, R.id.btn_cancel_plan, R.id.btn_cancel_model, R.id.btn_back_model, R.id.btn_back_reboot, R.id.btn_cancel_reboot, R.id.btn_cancel_pwd, R.id.btn_back_pwd, R.id.btn_cancel_screen, R.id.btn_back_screen,R.id.btn_back_plan -> {
                this.finish()
            }
            R.id.btn_save -> {
                var text = et_name_set.text.toString()
                SPUtil.putSP(Common.MACNAME, text)
                ToastUtils.s("保存成功")
                setResult(Activity.RESULT_OK)
                this.finish()
            }
            R.id.btn_save_volume -> {
//                SmdtManagerUtils.smdtSetVolume()
//                currentV.text = "当前音量: ${SmdtManagerUtils.smdtSetAndroidVolume(volume)}"

                ToastUtils.s("设置音量成功")
                SmdtManagerUtils.smdtSetAndroidVolume(volume)
                setResult(Activity.RESULT_OK)
                this.finish()
            }
            //清空设置
            R.id.btn_set_cls -> {
                var onh = on_h.text.toString()
                var onm = on_m.text.toString()
                var offh = off_h.text.toString()
                var offm = off_m.text.toString()
                SmdtManagerUtils.smdtSetTimingSwitchMachine("$offh:$offm", "$onh:$onm", "0")
                on_h.setText("")
                on_m.setText("")
                ToastUtils.s("清除成功")

            }
            //开关机时间保存设置
            R.id.btn_save_plan -> {
                var onh = on_h.text.toString()
                var onm = on_m.text.toString()
                var offh = off_h.text.toString()
                var offm = off_m.text.toString()

                if (TextUtils.isEmpty(onh) || TextUtils.isEmpty(onm) || TextUtils.isEmpty(offh) || TextUtils.isEmpty(
                        offm
                    )
                ) {
                    ToastUtils.s("数据不完整")
                } else {
                    SmdtManagerUtils.smdtSetTimingSwitchMachine("$offh:$offm", "$onh:$onm", "1")
                    var timeObject = HashMap<String, String>()
                    timeObject["off"] = "$offh:$offm"
                    timeObject["on"] = "$onh:$onm"
                    SPUtil.putSP(Common.BOOTTIME, "$onh:$onm")
                    SPUtil.putSP(Common.SHUTDOWN, "$offh:$offm")
                    SPUtil.putSP(Common.OPENTIME, Gson().toJson(timeObject))
                    ToastUtils.s("设置成功")
                    this.finish()
                }
            }
            R.id.btn_save_model -> {//连接模式
                if (model) {
                    SPUtil.putSP(Common.MOUDLE, "单机模式")
                } else {
                    SPUtil.putSP(Common.MOUDLE, "联机模式")
                }

                ToastUtils.s("设置成功")
                this.finish()
            }
            R.id.btn_save_reboot -> {//启动模式 应用守护
                if (b_protect) {
                    SPUtil.putSP(Common.PROTECT, "开")
                } else {
                    SPUtil.putSP(Common.PROTECT, "关")
                }
                if (b_reboot) {
                    SPUtil.putSP(Common.REBOOT, "开")
                } else {
                    SPUtil.putSP(Common.REBOOT, "关")
                }
                ToastUtils.s("设置成功")
                this.finish()
            }
            R.id.btn_save_pwd -> {//设置密码
                var pwd = pwd_et.text.toString()
                if(TextUtils.isEmpty(pwd).not()){
                    SPUtil.putSP(Common.PWD, pwd)
                    ToastUtils.s("密码成功")
                    this.finish()
                }else{
                    ToastUtils.s("请设置密码")
                }

            }
            R.id.btn_save_screen -> {//屏幕旋转角度保存

//                SmdtManagerUtils.setRotation(b_angle.toString())
                (ScrollActivity.scrollAsc as ScrollActivity).setScreenonChange(b_angle.toString())
//                SPUtil.putSP(Common.ANGLE, "$b_angle")
                ToastUtils.s("设置成功")
                this.finish()
            }
        }

    }
}
